# coding:utf-8
import requests
import threading
from queue import Queue
import logging

log = logging.getLogger(__name__)


def url_user_agent(proxy, url, timeout):
    proxies = {'http': proxy, 'https': proxy}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'
    }
    requests.get(url, headers=headers, proxies=proxies, timeout=timeout)


def check(proxy, queue, timeout):
    try:
        url = 'http://httpbin.org/get?show_env=1'
        url_user_agent(proxy, url, timeout)
    except Exception as e:
        log.error(e)
    else:
        queue.put(proxy)


def save_proxy(queue, save_list):
    while True:
        proxy = queue.get()
        if proxy == 'close':
            queue.task_done()
            break
        save_list.append(proxy)
        queue.task_done()


def filter_proxies(iterator, timeout):
    true_list = []
    queue = Queue()
    save_thread = threading.Thread(target=save_proxy, args=(queue, true_list))
    save_thread.start()
    threads = []
    for line in iterator:
        if not line:
            continue
        thread = threading.Thread(target=check, args=(line, queue, timeout))
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join(timeout=timeout)
    queue.put('close')
    queue.join()
    return true_list
